package com.lj.cloudDisk.controller;


import com.lj.cloudDisk.config.exception.SpaceException;
import com.lj.cloudDisk.pojo.UserDir;
import com.lj.cloudDisk.service.UserDirService;
import com.lj.cloudDisk.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@Api(value = "用户目录")
@RestController
@RequestMapping("/user/dir")
public class UserDirController {


    @Autowired
    UserDirService userDirService;

    @ApiOperation(value = "获取当前用户的目录下的文件列表")
    @GetMapping("/getUserDir/{id}")
    public R getUserDir(@PathVariable String id){
            UserDir userDir = userDirService.getUserDir(id);
            return R.success().data("dir",userDir);


    }


    @ApiOperation("新建文件夹")
    @PostMapping("/setUserDir")
    public R setUserDir(@RequestBody Map<String,Object> map){
        String userId = (String)map.get("userId");
        String createdName = (String)map.get("createdName");
        Long currentFolderId = Long.valueOf(map.get("currentFolderId").toString());
        UserDir  userDir = userDirService.setDirStruct(userId, createdName, currentFolderId);

        return R.success().data("dir",userDir);

    }

    @ApiOperation("重命名文件夹")
    @PostMapping("/updateDir")
    public R updateDir(@RequestBody Map<String,Object> map ){
        String userId = (String)map.get("userId");
        String rename = (String)map.get("rename");
        Long folderId = Long.valueOf(map.get("folderId").toString());
        String url = (String)map.get("url");

        log.debug("userId:{},rename:{},folderId:{}",userId,rename,folderId);

        UserDir userDir = userDirService.renameFolder(userId,rename,folderId,url);

        return R.success().data("userDir",userDir);
    }

    @ApiOperation("删除文件夹")
    @PostMapping("/deleteDir")
    public R deleteDir(@RequestBody Map<String,Object> map){
        String userId = (String)map.get("userId");
        Long folderId = Long.valueOf(map.get("folderId").toString());//父文件id
        String url = (String)map.get("url");//目录路径
        log.debug("userId:{},url:{},folderId:{}",userId,url,folderId);

       UserDir userDir = userDirService.delFolder(userId,url,folderId);
        return R.success().data("userDir",userDir);
    }
}
